package wpp;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import wpp.cfg.DataConfig;
import wpp.util.IOUtil;
import wpp.util.OSUtil;

public class WPPConfig {
	
	private static final String APP_TITLE = "Wallpaper Pipe";
	private static final Double APP_WIDTH = 1280.0;
	private static final Double APP_HEIGHT = 800.0;
	private static final Double WP_PREVIEW_WIDTH = 264.0;
	private static final Double WP_PREVIEW_HEIGHT = 165.0;
	
	
	private static final String APP_DATA_DIR_NAME = ".WallpaperPipe"; 
	private static File appDataDirectory;
	private static final String APP_CFG_FILE_NAME = "cfg.lovecll";
	private static File cfgFile;
	private static final String WP_PREVIEW_DIR = "wppreview";
	private static File wpPreviewDir;
	private static final String WP_DIR = "wp";
	private static File wpDir;

	public static void initWPPConfig() throws IOException {
		
		String homePath = OSUtil.getInstance().getUserHomeDirectory().getAbsolutePath();
		appDataDirectory = IOUtil.createDirIfNotExists(homePath + "/" + APP_DATA_DIR_NAME);
		wpPreviewDir = IOUtil.createDirIfNotExists(appDataDirectory.getAbsolutePath() + "/" + WP_PREVIEW_DIR);
		wpDir = IOUtil.createDirIfNotExists(appDataDirectory.getAbsolutePath() + "/" + WP_DIR);
		cfgFile = IOUtil.createFileIfNotExists(appDataDirectory.getAbsolutePath() + "/" + APP_CFG_FILE_NAME);
				
		DataConfig.load();
	}
	
	public static File getPreviewWPFile(String fileName) {
		return new File(WPPConfig.getWpPreviewDir().getAbsolutePath() + "/" + fileName);
	}
	
	public static void savePreviewWPFile(String fileName, InputStream in) throws IOException {
		IOUtil.saveFile(WPPConfig.getWpPreviewDir().getAbsolutePath() + "/" + fileName, in);
	}
	
	public static String getWpFilePath(String fileName) {
		return getWpDir().getAbsolutePath() + "/" + fileName;
	}
	
	public static File getWPPreviewFile(String fileName) {
		return new File(getWpPreviewDir().getAbsolutePath() + "/" + fileName);
	}
	
	public static String getAppTitle() {
		return APP_TITLE;
	}
	
	public static Double getAppWidth() {
		return APP_WIDTH;
	}
	
	public static Double getAppHeight() {
		return APP_HEIGHT;
	}
	
	public static Double getWpPreviewWidth() {
		return WP_PREVIEW_WIDTH;
	}

	public static Double getWpPreviewHeight() {
		return WP_PREVIEW_HEIGHT;
	}

	public static String getThemeCSSPath() {
		return WPPConfig.class.getResource("/view/css/WPP.css").toExternalForm();
	}
	
	public static String getMainCSSPath() {
		return WPPConfig.class.getResource("/view/css/win7glass.css").toExternalForm();
	}

	public static File getAppDataDirectory() {
		return appDataDirectory;
	}

	public static File getCfgFile() {
		return cfgFile;
	}

	public static File getWpPreviewDir() {
		return wpPreviewDir;
	}

	public static File getWpDir() {
		return wpDir;
	}		
	
}
